package marmalius.craft;

import marmalius.Player;
import marmalius.client;
import marmalius.player.SkillType;

public class SmeltingService {
	public static boolean isFurnace(int objectID) {
		switch (objectID) {
			case 2781:
			case 11666:
			case 9390:
				return true;
		}
		return false;
	}
	
	public static void ItemOnfurnace(Player player, int itemID) {
		if (!player.getInventory().hasItem(itemID))
			return ;
		
		if (!player.getInventory().haveSpaceForItem(2349, 1)) {
			player.sendPlayerMessage("You don't have enough space in your inventory.");
			return ;
		}
		switch (itemID) {
			case 436: //Copper
				if (!player.getInventory().hasItem(438)) //+ Tin
					return ;
			case 438: //Tin
				if (!player.getInventory().hasItem(436)) //+ Copper
					return ;
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(436, 1);
				player.getInventory().removeItem(438, 1);
				player.getInventory().addItem(2349, 1);
				player.sendPlayerMessage("You make a Bronze bar.");
				((client)player).addSkillXP(6.2, SkillType.SMITHING.getValue());
				break;
			case 440: //Iron
				if (player.getSkillLevel(SkillType.SMITHING) >= 30) { //Steel Bar
					if (player.getInventory().hasItemCount(438, 2)) {
						player.startAnimation(0x383);
						player.getInventory().removeItem(440, 1);
						player.getInventory().removeItem(438, 2);
						player.getInventory().addItem(2353, 1);
						player.sendPlayerMessage("You make a Stell bar.");
						((client)player).addSkillXP(17.5, SkillType.SMITHING.getValue());
						return ;
					}
				}
				if (player.getSkillLevel(SkillType.SMITHING) < 15) {
					player.sendPlayerMessage("Smithing Level too low (15 require).");
					return ;
				}
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(440, 1);
				if (Math.random() < getMaxChanceIron(player)) {
					player.getInventory().addItem(2351, 1);
					player.sendPlayerMessage("You make an Iron bar.");
				}
				else
					player.sendPlayerMessage("Iron ore lost.");
				((client)player).addSkillXP(12.5, SkillType.SMITHING.getValue());
				break;
			case 2892: //Elemental
				//TODO Must complete Elemental Workshop quest. 
				//TODO Can only be smithed using the special furnace and anvils in the Elemental Workshop.
				if (player.getSkillLevel(SkillType.SMITHING) < 20) {
					player.sendPlayerMessage("Smithing Level too low (20 require).");
					return ;
				}
				if (!player.getInventory().hasItemCount(438, 4)) //+ 4 Coal
					return ;
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(2892, 1);
				player.getInventory().removeItem(438, 4);
				player.getInventory().addItem(2893, 1);
				player.sendPlayerMessage("You make an Elemental metal.");
				((client)player).addSkillXP(7.5, SkillType.SMITHING.getValue());
				break;
			case 442: //Silver
				if (player.getSkillLevel(SkillType.SMITHING) < 20) {
					player.sendPlayerMessage("Smithing Level too low (20 require).");
					return ;
				}
				player.startAnimation(0x383);
				player.getInventory().removeItem(442, 1);
				player.getInventory().addItem(2355, 1);
				player.sendPlayerMessage("You make a Silver Bar.");
				((client)player).addSkillXP(13.7, SkillType.SMITHING.getValue());
				break;
			case 444: //Gold
				if (player.getSkillLevel(SkillType.SMITHING) < 40) {
					player.sendPlayerMessage("Smithing Level too low (40 require).");
					return ;
				}
				player.startAnimation(0x383);
				player.getInventory().removeItem(444, 1);
				player.getInventory().addItem(2357, 1);
				player.sendPlayerMessage("You make a Gold Bar.");
				((client)player).addSkillXP(30, SkillType.SMITHING.getValue());
				break;
			case 447: //Mithril
				if (player.getSkillLevel(SkillType.SMITHING) < 50) {
					player.sendPlayerMessage("Smithing Level too low (50 require).");
					return ;
				}
				if (!player.getInventory().hasItemCount(438, 4)) //+ 4 Coal
					return ;
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(447, 1);
				player.getInventory().removeItem(438, 4);
				player.getInventory().addItem(2359, 1);
				player.sendPlayerMessage("You make a Mithril Bar.");
				((client)player).addSkillXP(22.5, SkillType.SMITHING.getValue());
				break;
			case 449: //Adamantite
				if (player.getSkillLevel(SkillType.SMITHING) < 70) {
					player.sendPlayerMessage("Smithing Level too low (50 require).");
					return ;
				}
				if (!player.getInventory().hasItemCount(438, 6)) //+ 6 Coal
					return ;
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(449, 1);
				player.getInventory().removeItem(438, 6);
				player.getInventory().addItem(2361, 1);
				player.sendPlayerMessage("You make an Adamantite Bar.");
				((client)player).addSkillXP(37.5, SkillType.SMITHING.getValue());
				break;
			case 451: //Runite
				if (player.getSkillLevel(SkillType.SMITHING) < 85) {
					player.sendPlayerMessage("Smithing Level too low (50 require).");
					return ;
				}
				if (!player.getInventory().hasItemCount(438, 8)) //+ 8 Coal
					return ;
				
				player.startAnimation(0x383);
				player.getInventory().removeItem(451, 1);
				player.getInventory().removeItem(438, 8);
				player.getInventory().addItem(2363, 1);
				player.sendPlayerMessage("You make a Runite Bar.");
				((client)player).addSkillXP(50, SkillType.SMITHING.getValue());
		}
	}
	
	private static double getMaxChanceIron(Player player) {
		int SmithingLevel = player.getSkillLevel(SkillType.SMITHING);
		if (SmithingLevel >= 45)
			return 0.8;
		if (SmithingLevel > 15)
			return 0.5 + ((SmithingLevel - 15) * 0.01);
		return 0.5;
		
	}
}
